import { observable, action, autorun, toJS } from 'mobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { message, Modal } from 'antd';
import * as Apis from '../apis/highSeas';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { getSeasTree } from '../apis/highSeas';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class highSeasApproveListStore {
    @observable tableStore = new TableStore();
    @observable loading = false;
    @observable searchConditionForm = new WeaForm();
    @observable searchConditionComponents = {};
    @observable showSearchAd = false;
    @observable searchBaseParams = {};
    @observable showLeft = true;
    @observable rightMenu = [];
    @observable seasId = '';
    @observable treeData = [];
    @observable countsType = [];
    @observable counts = {};
    @observable selectedKey = 0;
    @observable logDialogVisible = false;
    @observable logType = 2;
    @observable topKey = 0;
    @observable hasApproveRight = false;
    @observable CrmName = '';// 客户名称
    @observable customerIds = '';// 勾选的客户ID
    @observable showModal = false; // 弹框显示
    @observable buttonDisabled = false;
    @observable verified = false;

    @action
    initDatas = (params = {}) => {
      this.tableStore = new TableStore();
      this.searchBaseParams = {};
      this.searchConditionForm.reset();
      this.loading = true;
      this.countsType = [];
      this.counts = {};
      this.treeData = [];
      this.seasId = '';


      Apis.approveRight({}).then(action((data) => {
        if (data.datas) {
          this.verified = true;
          this.hasApproveRight = true;
          this.getTableData({ ...params, onlysessionkey: false });
          this.getSeasTree();
        } else {
          this.hasSeasRight = false;
          this.verified = true;
        }
      }));
    }

    @action
    doSearch = () => {
      this.tableStore = new TableStore();
      const that = this;
      this.loading = true;
      this.setShowSearchAd(false);
      const searchParamsAd = this.searchConditionForm.getFormParams();
      // searchParamsAd里没有uncontact，uncontact_1时，需要将searchBaseParams里的值清掉
      if (!searchParamsAd.uncontact) {
        delete that.searchBaseParams.uncontact;
      }
      if (!searchParamsAd.uncontact_1) {
        delete that.searchBaseParams.uncontact_1;
      }
      const requestParams = { ...this.searchBaseParams, ...searchParamsAd, seasId: this.seasId, onlysessionkey: true };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
          delete that.searchBaseParams[item];
        }
      }
      this.searchBaseParams = { ...this.searchBaseParams, ...requestParams };
      this.getTableData(this.searchBaseParams);
    }

    getTableData = (params = {}) => {
      if (this.topKey == 0) {
        Apis.seasApproveList(params).then((data) => {
          if (data.status == '0') {
            message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
          } else {
            this.tableStore.getDatas(data.datas, 1);
            if (!params.onlysessionkey) {
              this.searchConditionForm = new WeaForm();
              this.searchConditionComponents = data.formItemGroupList;
              this.searchConditionForm.initFormFields(this.searchConditionComponents);

              this.rightMenu = data.rightMenu;
            }
          }
          this.loading = false;
        });
      } else if (this.topKey == 1) {
        Apis.seasDispenseList(params).then((data) => {
          if (data.status == '0') {
            message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
          } else {
            this.tableStore.getDatas(data.datas, 1);
            if (!params.onlysessionkey) {
              this.searchConditionForm = new WeaForm();
              this.searchConditionComponents = data.formItemGroupList;
              this.searchConditionForm.initFormFields(this.searchConditionComponents);

              this.rightMenu = data.rightMenu;
            }
          }
          this.loading = false;
        });
      }
    }

    @action
    tableReload = () => {
      this.tableStore.getDatas(this.tableSessionKey, this.tableStore.current);
    }

    @action
    setShowSearchAd = (bool = false) => {
      this.showSearchAd = bool;
    }

    clearAdFields = () => {
      this.searchConditionForm.reset();
    }

    @action
    setSearchKey = (v = '') => {
      try {
        this.searchBaseParams.name = v;
        const searchKeyField = {};
        searchKeyField.name = v;
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange = (fieldObj) => {
      if (fieldObj.name) {
        this.searchBaseParams = { ...this.searchBaseParams, name: fieldObj.name.value };
      }
    }

    @action
    isShowLeft = (bl) => {
      this.showLeft = bl;
    }

    @action
    getSeasTree = (params = {}) => {
      if (this.topKey == 0) {
        Apis.seasApproveTree(params).then((data) => {
          if (data.status == '0') {
            message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
          } else {
            this.treeData = data.datas;
            this.countsType = data.countsType;
            this.counts = data.counts;
          }
        });
      } else if (this.topKey == 1) {
        Apis.seasDispenseTree(params).then((data) => {
          if (data.status == '0') {
            message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
          } else {
            this.treeData = data.datas;
            this.countsType = data.countsType;
            this.counts = data.counts;
          }
        });
      }
    }

    @action
    seasId = (seasId) => {
      this.seasId = seasId;
    }

    @action
    controlOperateDialog = (params, action) => {
      const requestParams = { ids: `${params}`, action };
      let title = '';
      if (action == 'agree') {
        title = getLabel(382012, '是否确认同意？');
      } else if (action == 'disagree') {
        title = getLabel(33475, '是否确认不同意？');
      }
      const that = this;
      Modal.confirm({
        title,
        onOk() {
          Apis.getHighSeasInfo(requestParams).then((data) => {
            message.success(`成功处理${data.datas}条申请！`);
            that.tableReload();
          });
        },
        onCancel() {},
      });
    }

    @action
    approveCustomer = (params, agree) => {
      Apis.approveRight({}).then(action((data) => {
        if (data.datas) {
          this.buttonDisabled = true;
          const requestParams = { approveIds: `${params}`, agree };
          this.loading = true;
          Apis.approveCustomer(requestParams).then((data) => {
            if (data.status == '0') {
              message.error(`${getLabel(83437, '出现错误！') + data.error}!`);
            } else {
              message.success(`成功处理${data.datas}个！`);
              this.tableReload();
              this.getSeasTree();
            }
            this.loading = false;
            this.buttonDisabled = false;
          });
        } else {
          message.error(getLabel(2012, '对不起，您暂时没有权限！'));
          this.hasSeasRight = false;
        }
      }));
    }

    @action
    showLog = () => {
      this.logDialogVisible = true;
    }

    @action
    changeLogDialog = () => {
      this.logDialogVisible = false;
    }

    @action
    resetParams = () => {
      for (const item in this.searchBaseParams) {
        delete this.searchBaseParams[item];
      }
      this.searchConditionForm.reset();
    }

    // 显示弹窗
    showCustomerModal = (bool, ids) => {
      Apis.approveRight({}).then(action((data) => {
        if (data.datas) {
          this.buttonDisabled = true;
          this.customerIds = ids;
          this.showModal = bool;
          if (bool) {
            Apis.getNameByseasCustomerid({ seasCustomerId: ids }).then((data) => {
              this.CrmName = data.datas;
            });
          } else {
            this.buttonDisabled = false;
          }
        } else {
          message.error(getLabel(2012, '对不起，您暂时没有权限！'));
          this.hasSeasRight = false;
        }
      }));
    }
    // 分配客户经理
    saveCustomer=(params = {}) => {
      this.loading = true;
      this.showModal = false;
      Apis.dispenseCustomer(params).then((data) => {
        this.customerIds = '';
        this.tableReload();
        this.getSeasTree();
        this.loading = false;
        this.buttonDisabled = false;
      });
    }

    @action
    changeTopTab = (theKey) => {
      this.topKey = theKey;
      if (theKey == 0) {
        this.logType = 2;
      } else if (theKey == 1) {
        this.logType = 3;
      }
      this.initDatas();
    }
}
